#include"cmd1.h"
static int stackPush(char **now_addr,char *dir_name,int *size){
    now_addr[*size]=dir_name;
    (*size)++;
    return 0;
}
static int stackPop(char**now_addr,int *size){
    if((*size)==0){
        printf("in the root dir\n");
        return 1;
    }
    (*size)--;
    char *p=now_addr[*size];
    free(p);
    now_addr[*size]=NULL;
    return 0;
}
int compare(const void*a,const void*b){
    return strcmp(*(const char**)a,*(const char**)b);
}
int cdFunc(char*path,int path_size,char *now_addr){
    if(path_size==0){
        memcpy(now_addr,".",1);
        return 0;
    }
    char*tmp=(char*)calloc(strlen(now_addr),sizeof(char));
    memcpy(tmp,now_addr,strlen(now_addr));
    char**spath=(char**)calloc(path_size,sizeof(char*));
    for(int i=0;i<path_size;i++){
        char*buf=(char*)calloc(1024,sizeof(char));
        spath[i]=buf;
    }
    char**now_spath=(char**)calloc(path_size+strlen(now_addr),sizeof(char*));
    for(int i=0;i<path_size+strlen(now_addr);i++){
        char*buf=(char*)calloc(1024,sizeof(char));
        now_spath[i]=buf;
    }
    int pathsize;
    pathSeparation(path,spath,&pathsize);
    int now_addr_size;
    pathSeparation(now_addr,now_spath,&now_addr_size);
    for(int i=0;i<pathsize;i++){
        if(strcmp(spath[i],"..")==0){
            free(spath[i]);
            stackPop(now_spath,&now_addr_size);
        }
        else if(strcmp(spath[i],".")==0){
            free(spath[i]);
            continue;
        }
        else{
            stackPush(now_spath,spath[i],&now_addr_size);
        }
    }
    bzero(now_addr,strlen(now_addr));
    if(now_addr_size==0){
        memcpy(now_addr,".",1);
        return 0;
    }
    strcat(now_addr,now_spath[0]);
    for(int i=1;i<now_addr_size;i++){
        strcat(now_addr,"/");
        strncat(now_addr,now_spath[i],strlen(now_spath[i]));
    }
    printf("now_addr=%s\n",now_addr);
    int res=access(now_addr,F_OK);
    if(res==-1){
        printf("Can not find this file\n");
        memcpy(now_addr,tmp,strlen(tmp));
        free(tmp);
        return 1;
    }
    free(spath);
    for(int i=0;i<now_addr_size;i++){
        free(now_spath[i]);
    }
    free(tmp);
    free(now_spath);
    return 0;
}
int lsFunc(char *now_addr,char* path,int path_size,char* buf){
    cdFunc(path,path_size,now_addr);
    DIR *dir=opendir(now_addr);
    if(dir==NULL){
        printf("opendir field\n");
        return 1;
    }
    struct dirent *entry;
    char *manu[1024]={0};
    int pmanu=0;
    while((entry=readdir(dir))!=NULL){
        if(strcmp(entry->d_name,".")==0||strcmp(entry->d_name,"..")==0){
            continue;
        }
        manu[pmanu]=entry->d_name;
        pmanu++;
    }
    qsort(manu,pmanu,sizeof(char*),compare);
    for(int i=0;i<pmanu;i++){
        strncat(buf,manu[i],strlen(manu[i]));
        strcat(buf,"\n");
    }
    closedir(dir);
    return 0;
}
int pathSeparation(char*path,char**spath,int *size){
    if(spath==NULL){
        printf("spatch no space\n");
        exit(1);
    }
    *size=0;
    char *p=path;
        char**q=spath;
        while(*p==' '){
            p++;
        }
    while(p!=NULL&&*p!=0){
        if(*p=='/'){
            p++;
        }
        else{
            while(*p==' '){
                p++;
            }
            int i=0;
            while(p!=NULL&&*p!=0&&*p!='/'){
                (*q)[i]=*p;
                i++;
                p++;
            }
            q++;
            (*size)++;
        }
    }
    return 0;
}
